package com.flc.realtime.config;

import com.flc.gateway.utils.PropertyFactory;

/**
 * @author haizi
 * @date 2018/1/2
 */
public class AppConfig {

    public RocketMqCfg getRocketMqCfg() {
        return rocketMqCfg;
    }

    public RedisCfg getRedisCfg() {
        return redisCfg;
    }

    private RocketMqCfg rocketMqCfg;
    private RedisCfg redisCfg;

    public class RocketMqCfg {

        public String getGroupName() {
            return groupName;
        }

        public String getNameServerAddr() {
            return nameServerAddr;
        }

        public String getDiagnosisTopic() {
            return diagnosisTopic;
        }

        public String getLoginTopic() {
            return loginTopic;
        }

        public String getReportTopic() {
            return reportTopic;
        }

        public String getCmdTopic() {
            return cmdTopic;
        }

        public String getTrajectoryTopic() {
            return trajectoryTopic;
        }

        String groupName;
        String nameServerAddr;
        String diagnosisTopic;
        String loginTopic;
        String reportTopic;
        String trajectoryTopic;
        String cmdTopic;

    }

    public class RedisCfg {

        public Integer getMaxIdle() {
            return maxIdle;
        }

        public Integer getMaxTotal() {
            return maxTotal;
        }

        public Integer getMaxWaitMillis() {
            return maxWaitMillis;
        }

        public String getHost() {
            return host;
        }

        public Integer getPort() {
            return port;
        }

        Integer maxIdle;
        Integer maxTotal;
        Integer maxWaitMillis;
        String  host;
        Integer port;
    }

    private AppConfig() {
        String propfileName = "Realtime.properties";

        PropertyFactory factory = new PropertyFactory(propfileName);
        rocketMqCfg = new RocketMqCfg();
        redisCfg = new RedisCfg();

        rocketMqCfg.groupName = factory.getValue("ROCKETMQ.GROUPNAME");
        rocketMqCfg.nameServerAddr = factory.getValue("ROCKETMQ.NAMESERVERADDR");
        rocketMqCfg.diagnosisTopic = factory.getValue("ROCKETMQ.TOPIC.DIAGNOSIS");
        rocketMqCfg.loginTopic = factory.getValue("ROCKETMQ.TOPIC.LOGIN");
        rocketMqCfg.reportTopic = factory.getValue("ROCKETMQ.TOPIC.REPORT");
        rocketMqCfg.trajectoryTopic = factory.getValue("ROCKETMQ.TOPIC.TRAJECTORY");
        rocketMqCfg.cmdTopic = factory.getValue("ROCKETMQ.TOPIC.CMD");

        redisCfg.maxIdle = Integer.parseInt(factory.getValue("JEDIS.MAXIDLE"));
        redisCfg.maxTotal = Integer.parseInt(factory.getValue("JEDIS.MAXTOTAL"));
        redisCfg.maxWaitMillis = Integer.parseInt(factory.getValue("JEDIS.MAXWAITMILLIS"));
        redisCfg.host = factory.getValue("REDIS.HOST");
        redisCfg.port = Integer.parseInt(factory.getValue("REDIS.PORT"));
    }

    private static volatile AppConfig instance;

    public static AppConfig getInstance() {
        if (instance == null) {
            synchronized(AppConfig.class) {
                if (instance == null) {
                    instance = new AppConfig();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        RedisCfg redisCfg = AppConfig.getInstance().getRedisCfg();
        System.out.println(redisCfg.getHost());

    }

}
